package com.funo.ehealth.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.funo.ehealth.bean.UserUpDown;
import com.funo.ehealth.exception.SQLDOException;
import com.funo.ehealth.main.BussinessMgr;
import com.funo.ehealth.util.DBUtil;
import com.funo.ehealth.util.LogUtil;
import com.funo.ehealth.util.StringUtil;

/** 
 * 用户上下行Dao
 * 
 * @version V1.1
 * @author zhangguangying
 * @date 2012-11-17
 */
public class UserUpDownDao {
	
	/**
	 * UserUpDownDao日志记录器
	 */
	private static final Log log = LogFactory.getLog(UserUpDownDao.class);

	/**
	 * 查询发送给CP的失败上行信息
	 * @return List<UserUpDown>
	 */
	public void queryFailMsgToCP(List userUpDownList){
		String sql = "select t.up_time,t.ID,t.SRC_ID,t.DEST_ID,t.CONTENT,s.PRODUCT_URL from USERUPDOWN t,servicelist s where t.SERVICELIST_ID=s.id and t.uptocp=1 and t.is_process=0 and sysdate<=(t.UP_TIME+interval '"+BussinessMgr.upToCpLimitMinutes+"' MINUTE)";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet res = null;
		UserUpDown userUpDown = null;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, sql);
		
			LogUtil.debug(log,"queryFailMsgToCP,参数：{sql:{}}", sql);
			res = pstmt.executeQuery();
			while (res.next()) {
				userUpDown = new UserUpDown();
				userUpDown.setId(res.getLong("ID"));
				userUpDown.setSrcId(res.getString("SRC_ID"));
				userUpDown.setDestId(res.getString("DEST_ID"));
				userUpDown.setContent(res.getString("CONTENT"));
				userUpDown.setProductUrl(res.getString("PRODUCT_URL"));
				userUpDownList.add(userUpDown);
			}
		} catch (Exception e) {
			LogUtil.error(log, e, "queryFailMsgToCP异常,参数：{sql:{}}", sql);
//			throw new SQLDOException(StringUtil.handleString("queryFailMsgToCP异常,参数：{sql:{}}", sql), e);
		} finally {
			DBUtil.closeAll(conn, pstmt, res);
		}
	}
	
	/**
	 * 修改表的ID对应的IS_PROCESS
	 * @param isProcess 是否处理  0 未处理  1:已处理  2:处理中 -1:下发成功
	 * @param id USERUPDOWN表Id
	 * @return int
	 */
	public int updateIsProcess(int isProcess,long id){
		String sql = "update USERUPDOWN set IS_PROCESS=? where id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		int res = 0;
		
		try {
			conn = DBUtil.getConnection();
			pstmt = DBUtil.getPreparedStatement(conn, sql);
			
			LogUtil.debug(log,"updateIsProcess,参数：{sql:{},isProcess:{},id:{}}", sql,isProcess,id);
			pstmt.setInt(1, isProcess);
			pstmt.setLong(2, id);
			res = pstmt.executeUpdate();
		} catch (SQLException e) {
//			LogUtil.error(log, e, "updateIsProcess异常,参数：{sql:{},isProcess:{},id:{}}", sql,isProcess,id);
			throw new SQLDOException(StringUtil.handleString("updateIsProcess异常,参数：{sql:{},isProcess:{},id:{}}", sql,isProcess,id), e);
		} finally {
			DBUtil.closeAll(conn, pstmt, null);
		}
		return res;
	}
}
